package de.ama.crawler;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: x
 * Date: 09.07.11
 * Time: 20:24
 * To change this template use File | Settings | File Templates.
 */
public class Migrate {
    private XStream xstream = new XStream(new DomDriver());
    private File dir = null;
    private File dbFile = null;
    private List<Entry> entries = new ArrayList<Entry>();


    public Migrate() {
    }


    public void read(File dir, String fileName) {
        xstream.alias("movie", Entry.class);
        dbFile = new File(dir, fileName);

        if (dbFile.exists()) {
            try {
                InputStream is = new FileInputStream(dbFile);
                entries.clear();
                entries.addAll((List<Entry>) xstream.fromXML(is));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public void write() {
        for (Entry e : entries) {
            e.save();
        }
    }

    public static void main(String[] args) {
        String bin = System.getProperty("user.dir");
        String parent = new File(bin).getParent();

        Migrate migrate = new Migrate();
        migrate.read(new File(parent, "data"), "!movies.xml");
        migrate.write();
    }

}
